<?php
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 akademik_kalender_akademik
	 akademik_kalender_akademik_add
	 akademik_kalender_akademik_edit
	 akademik_kalender_akademik_del	 
	 */
	class MainController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";		
		public function init() {
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");				
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_kalender_akademik");
			$this->subMenu = "Kalender Akademik";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"","tahun_ajaran_id"=>"","tahun_ajaran_name"=>"");			
			$this->render('index',$data);
		}
 
		public function actiondetailList(){	
		 
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
			$month = Yii::app()->request->getParam("month","");	
			$year = Yii::app()->request->getParam("year","");
			$filter_name = Yii::app()->request->getParam("filter_name","");			
			$mcon= Yii::app()->db;
			$sql1='select * from m_kalender_akademik where department_id = "'.$department_id.'" and tahun_ajaran_id='.$tahun_ajaran_id. ' and name="'.$filter_name.'" and ((('.$year.' between Extract(year from start_date) and Extract(year from end_date))   and   ('.$month.' between Extract(month from start_date) and Extract(month from end_date)))   or   (('.$month.' =  Extract(month from start_date)) and ('.$year.' = Extract(year from start_date))   or   (('.$month.' =  Extract(month from end_date)) and ('.$year.' = Extract(year from end_date))))) ';
			$mKalenderAkademik=$mcon->createCommand($sql1);
 					
			 
			$mKalenderAkademik=$mKalenderAkademik->queryAll();	
 	
		 	$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] =  $mKalenderAkademik;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			 
			 
		}
	 
		
		public function actionedit(){		
 
			$op = Yii::app()->request->getParam("op","add");
 			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				
				LoginUser::getPermission("akademik_kalender_akademik_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$kalenderakademik = new MKalenderAkademik;
				$kalenderakademik->department_id= Yii::app()->request->getParam("department_id","");	
				$kalenderakademik->tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
				$kalenderakademik->name=  Yii::app()->request->getParam("name","");
				$start_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("start_date","01-Jan-2000")),"Y-m-d");				
				$end_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("end_date","01-Jan-2000")),"Y-m-d");				
				$kalenderakademik->start_date=$start_date;
				$kalenderakademik->end_date=$end_date;
				$kalenderakademik->detail_activity=Yii::app()->request->getParam("detail_activity","");	
				//$kalenderakademik->is_active=$status;
				if ($kalenderakademik->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($kalenderakademik->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_kalender_akademik_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$kalenderakademik = MKalenderAkademik::model()->find("id=:id",array(":id"=>$id));
				$kalenderakademik->department_id= Yii::app()->request->getParam("department_id","");	
				$kalenderakademik->tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
				$kalenderakademik->name=  Yii::app()->request->getParam("name","");
				$start_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("start_date","01-Jan-2000")),"Y-m-d");				
				$end_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("end_date","01-Jan-2000")),"Y-m-d");				
				$kalenderakademik->start_date=$start_date;
				$kalenderakademik->end_date=$end_date;	
				$kalenderakademik->detail_activity=Yii::app()->request->getParam("detail_activity","");						
				//$kalenderakademik->is_active=$status;
				if ($kalenderakademik->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($kalenderakademik->getErrors());
				}				
			}else if ($op == "del"){
				if (!LoginUser::getPermission("akademik_kalender_akademik_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_kalender_akademik where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			$start_date = Yii::app()->request->getParam("start_date","");						
			$end_date = Yii::app()->request->getParam("end_date","");									
			$name = Yii::app()->request->getParam("name","");			
			$detail_activity = Yii::app()->request->getParam("detail_activity","");	
			$filter_name = Yii::app()->request->getParam("filter_name","");								
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"tahun_ajaran_id" =>$tahun_ajaran_id,"name"=>$name,"detail_activity"=>$detail_activity,"start_date"=>$start_date,"end_date"=>$end_date);
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_kalender_akademik_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_kalender_akademik_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
//				$rsJadwalPelajaran = MJadwalPelajaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$mcon= Yii::app()->db;
				$sql1='select * from m_kalender_akademik where id='.$id.' order by id';
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();	
				$json = CJSON::encode($rsDep);
				$arrdata = json_decode($json,true);
				if (count($arrdata) > 0){
					for ($x=0; $x<count($arrdata); $x++)
					{				
						$data["tahun_ajaran_id"] =$arrdata[$x]['tahun_ajaran_id'];	
						$data["department_id"] = $arrdata[$x]['department_id'];									
						$start_date = date_format(date_create_from_format( "Y-m-d", $arrdata[$x]['start_date']),"d-M-Y");						
						$data["start_date"] = $start_date;							
						$end_date = date_format(date_create_from_format( "Y-m-d", $arrdata[$x]['end_date']),"d-M-Y");						
						$data["end_date"] =$end_date;							
						$data["name"] = $arrdata[$x]['name'];			
						$data["detail_activity"] = $arrdata[$x]['detail_activity'];	
					}
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>